/*
 *   Copyright (C) 2007 by Andrzej Oczkowicz
 *   andrew_lz@poczta.fm
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *   KMediaCatalog :)
 *
 */
#ifndef KMEDIA_H
#define KMEDIA_H

#include <QObject>
#include <QFile>
#include <QtCore>
#include <QWidget>
#include <QDialog>
#include <QFileInfo>
#include <QTime>

#include <blkid/blkid.h>
#include ".ui/ui_dmedia.h"


class dkMedia;

/**
	@author Andrzej Oczkowicz <andrew_lz@poczta.fm> 
*/
class kMedia : public QObject
{
Q_OBJECT
public:
    kMedia(QObject *parent = 0);
    ~kMedia();

public:
  typedef enum mediaState {ST_UNKNOWN, ST_MOUNTED, ST_NO_MOUNTED, ST_NODEV};
  typedef enum mediaType  {MT_UNKNOWN, MT_HDD, MT_CDROM, MT_DVD, MT_FLASH, MT_NET, MT_FLOP, MT_ANOTHER};
  typedef struct mediaItem{
    QString device;
    QString fs;
    QString mountPoint;
    QString label;
    QString uid;
    mediaState state;
    mediaType type;
  };

  typedef struct rawItem{
    QString device;
    QString mountPoint;
    QString fs;
    QString label;
    QString uid;
  };

  typedef QList<rawItem> kDrives;
  typedef QList<mediaItem> kDrivesInfo;

private:
  kDrives fstabRaw;
  kDrives mtabRaw;
  kDrivesInfo cacheInfo;
  rawItem * itemDefault(const QString&dev);
  dkMedia *dlgMedia;

public:
  void prepareCache();
  int newScan();
  bool isCached(){ return cacheInfo.size() > 0; };
  bool isMounted(const QString&dev);
  mediaState devState(const QString&dev);
  mediaType devType(rawItem*item);
  QString devIcon(mediaType t, mediaState s = ST_MOUNTED);
  rawItem * itemByDev(const QString&dev, kDrives&info);
  kDrivesInfo*drives(){ return &cacheInfo; };
  QString devTypeName(mediaType t);
  const mediaItem *driveByIdx(int idx);
  static QString mTab(){ return QString("/etc/mtab"); };
  static QString fsTab(){ return QString("/etc/fstab"); };
  static QStringList invalidFs();
  static bool readFile(const QString&file, kDrives&items);
  static bool blkDevInfo(const QString&dev, QString &label, QString &uid);

};



class dkMedia : public QDialog, private Ui::formMedia
{
  Q_OBJECT
  private:
    kMedia *media;

  private:
    void updateScanButton();

  public:
     dkMedia(kMedia*parent);
    ~dkMedia();

  public slots:
     void setDrives();

  public slots:
    void on_listDevices_itemActivated( QListWidgetItem * item );
    void on_listDevices_currentItemChanged ( QListWidgetItem * current, QListWidgetItem * previous );
    void on_spinBoxNum_valueChanged ( int i );
    void on_buttonBrowse_clicked ( bool checked = false );
    void on_buttonScan_clicked ( bool checked = false );
    void on_buttonCancel_clicked ( bool checked = false );
    void on_tabSelectSource_currentChanged ( int index );
    void on_editScanDir_textChanged ( const QString & text );
};

#endif
